package com.example.home.model

import com.blankj.utilcode.util.GsonUtils
import com.example.home.api.HomeApiServer
import com.example.entity.NetDataEntity
import com.example.home.entity.discover.Banner2CardEntity
import com.example.home.entity.discover.BrieCardEntity
import com.example.home.entity.discover.CategoryCardEntity
import com.example.home.entity.discover.SubjectCardEntity
import com.example.home.entity.discover.TopBannerEntity
import com.example.home.entity.nominate.FollowCardEntity
import com.example.home.entity.nominate.SquareCardCollectionEntity
import com.example.home.entity.nominate.TextCardEntity
import com.example.home.entity.nominate.VideoSmallCardEntity
import com.example.network.ApiUrl
import com.example.network.BaseResponse
import org.json.JSONObject
import javax.inject.Inject

/**
 * 首页的数据仓库
 * */
class HomeDataRepository @Inject constructor(private val apiServer : HomeApiServer) {

    /**
     * 获取推荐列表数据
     * */
    suspend fun getNominateData(url : String=ApiUrl.HOME_NOMINATE_URL):BaseResponse{
        var requestBody =apiServer.getNominateList(url)
        var resultJsonObj = JSONObject(requestBody.string())

        //封装List数据
        var itemList = mutableListOf<NetDataEntity>()
        resultJsonObj.optJSONArray("itemList").let {
            for (i in 0 until it.length()){
                var itemJsonObj = it[i] as JSONObject
               var type = itemJsonObj.optString("type")
               when(type){
                   "squareCardCollection"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),SquareCardCollectionEntity::class.java)))
                   "textCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),TextCardEntity::class.java)))
                   "VideoSmallCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),VideoSmallCardEntity::class.java)))
                   "followCard"->itemList.add(NetDataEntity(type, GsonUtils.fromJson(itemJsonObj.toString(),FollowCardEntity::class.java)))
               }
            }
        }
        return BaseResponse(
            count = resultJsonObj.optInt("count"),
            total = resultJsonObj.optInt("total"),
            itemMap=itemList,
            nextPageUrl = resultJsonObj.optString("nextPageUrl"),
            adExist = resultJsonObj.optBoolean("adExist")
        )
    }


    /**
     * 获取发现列表数据
     */
    suspend fun getDiscoverData(url : String = ApiUrl.HOME_DISCOVER_URL): BaseResponse{
        var responseBody = apiServer.getDiscoverList(url)
        var resultJsonObj = JSONObject(responseBody.string())
        var itemList = mutableListOf<NetDataEntity>()
        resultJsonObj.optJSONArray("itemList").let {
            for (i in 0 until it.length()){
                var itemJsonObj = it[i] as JSONObject
                var type = itemJsonObj.optString("type")
                when(type){
                    "horizontalScrollCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),TopBannerEntity::class.java)))
                    "specialSquareCardCollection"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),CategoryCardEntity::class.java)))
                    "columnCardList"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),SubjectCardEntity::class.java)))
                    "textCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),TextCardEntity::class.java)))
                    "banner2"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),Banner2CardEntity::class.java)))
                    "videoSmallCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),VideoSmallCardEntity::class.java)))
                    "briefCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),BrieCardEntity::class.java)))
                }
            }
        }
        return BaseResponse(
            count = resultJsonObj.optInt("count"),
            total = resultJsonObj.optInt("total"),
            itemMap=itemList,
            nextPageUrl = resultJsonObj.optString("nextPageUrl"),
            adExist = resultJsonObj.optBoolean("adExist")
        )
    }


    /**
     * 获取每日列表数据
     */
    suspend fun getDailyData(url : String = ApiUrl.HOME_DAILY_URL): BaseResponse{
        var responseBody = apiServer.getDailyList(url)
        var resultJsonObj = JSONObject(responseBody.string())
        var itemList = mutableListOf<NetDataEntity>()
        resultJsonObj.optJSONArray("itemList").let {
            for (i in 0 until it.length()){
                var itemJsonObj = it[i] as JSONObject
                var type = itemJsonObj.optString("type")
                when(type){
                    "textCard"->itemList.add(NetDataEntity(type,GsonUtils.fromJson(itemJsonObj.toString(),TextCardEntity::class.java)))
                    "followCard"->itemList.add(NetDataEntity(type, GsonUtils.fromJson(itemJsonObj.toString(),FollowCardEntity::class.java)))
                }
            }
        }

        return BaseResponse(
            count = resultJsonObj.optInt("count"),
            total = resultJsonObj.optInt("total"),
            itemMap=itemList,
            nextPageUrl = resultJsonObj.optString("nextPageUrl"),
            adExist = resultJsonObj.optBoolean("adExist")
        )
    }

}